<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class AddNode extends HTML_QuickForm {
  private $tpl;
  private $section;

  function __construct($tpl,$section) {
    $this->tpl = $tpl;
    $this->section = $section;

    if($this->tpl->get_template_vars('modify')) {
      $sec = $section->getSections($section->getId());
      $defaults = array('node' => $sec['name'],
                        'alias' => $sec['alias'],
                        'description' => $sec['description']);
      $this->setDefaults($defaults);
    }

    parent::HTML_QuickForm('addnode');
    $this->addElement('header', 'header', 'addnode');
    $this->addElement('text', 'node', _('node name'), array('size' => 30, 'maxlength' => 100));
    $this->addElement('text', 'alias', _('node alias'), array('size' => 30, 'maxlength' => 255));
    $this->addElement('text', 'description', _('node description'), array('size' => 50, 'maxlenght' => 255));
    $this->addElement('submit', 'btn_Submit', _('submit'));

    $this->applyFilter('__ALL__', 'trim');
    $this->applyFilter('node', 'strtolower');
    $this->applyFilter('alias', 'strtolower');
    $this->registerRule('check_exists', 'function', 'checkExists', $this);
    $this->addRule('node', _('node required'), 'required');
    $this->addRule('node', _('node characters not allowed'), 'alphanumeric');
    $this->addRule('node', _('node already exists'), 'check_exists');
    $this->addRule('alias', _('node characters not allowed'), 'regex', '/^[[:alnum:][:space:]]+$/');
    $this->addRule('alias', _('node alias and name must be unique'), 'check_exists');
  }

  function checkExists() {
    $this->section->setName($this->getSubmitValue('node'));
    $this->section->setAlias($this->getSubmitValue('alias'));
    if($this->tpl->get_template_vars('modify')) {
      return $this->section->checkExists($op = 'modify');
    } else {
      return $this->section->checkExists($op = 'node');
    }
  }

  function displayForm() {
    $this->renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);
   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->section->getMsg());
  }

  function process() {
    if($this->tpl->get_template_vars('modify')) {
      $this->section->modify($this->getSubmitValues());
    } else {
      $this->section->addNode($this->getSubmitValues());
    }
    $this->tpl->assign('result',true);
    $this->tpl->assign('msg',$this->section->getMsg());
  }

} // end of class

?>